package com.openness.crawler.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openness.crawler.crawler.CrawlConfig;
import com.openness.crawler.util.Constants;


/**
 * @author Rain
 * @version 2013-5-21 下午6:11:03
 */

public class HBaseTablePool {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(HBaseTablePool.class);

	private static Configuration conf = new Configuration();

	private static HTablePool pool;

	public static Configuration getConf() {
		return conf;
	}

	public static void initialize(CrawlConfig config) {
		conf.set(Constants.HBASE_MASTER, config.getHbaseMaster());
		conf.set(Constants.HBASE_ZOOKEEPER_QUORUM,
				config.getHbaseZooKeeperQuorum());
		conf.set(Constants.HBASE_ZOOKEEPER_PROPERTY_CLIENTPORT,
				config.getHbaseZooKeeperPropertyClientPort());

		pool = new HTablePool(conf, config.getHbaseTablePoolSize());
	}

	public static HTableInterface get(String tableName) {
		return pool.getTable(tableName);
	}

	public static void put(HTableInterface table) {
		if (table != null) {
			try {
				table.close();
			} catch (IOException e) {
				LOGGER.error("HBaseTablePool put table error: " + e.toString());
			}
		}
	}

	public static void shutdown() throws IOException {
		if (pool != null) {
			pool.close();
		}
	}

}
